package com.linjiahao.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.educms.entity.CrmBanner;
import com.linjiahao.educms.service.CrmBannerService;
import com.linjiahao.educms.vo.BannerQuery;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-01
 */
@RestController
@RequestMapping("/cmsservice/banner")
public class CrmBannerController {


    @Autowired
    private CrmBannerService bannerService;

    @ApiOperation(value = "获取Banner分页列表")
    @PostMapping("{page}/{limit}")
    public ResultMap index(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "bannerQuery", value = "查询对象", required = false)
                               @RequestBody BannerQuery bannerQuery) {
        Page<CrmBanner> pageParam = new Page<>(page, limit);
        bannerService.pageQuery(pageParam, bannerQuery);
        List<CrmBanner> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  ResultMap.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public ResultMap get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return ResultMap.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("save")
    @CacheEvict(value = "banner", allEntries=true)
    public ResultMap save(@RequestBody CrmBanner banner) {
        bannerService.save(banner);
        return ResultMap.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update/{id}")
    @CacheEvict(value = "banner", allEntries=true)
    public ResultMap updateById(@PathVariable String id,@RequestBody CrmBanner banner) {
        banner.setId(id);
        bannerService.updateById(banner);
        return ResultMap.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    @CacheEvict(value = "banner", allEntries=true)
    public ResultMap remove(@PathVariable String id) {
        bannerService.removeById(id);
        return ResultMap.ok();
    }
}

